<template>
  <el-col class="app-v-center app-column" style="overflow-y: auto;">
    <el-row class="app-h-48px app-fill-width app-v-center pl-4 app-bb-1px-l2" style="background: #009999" :gutter="10">
      <el-button
        size="mini"
        @click="toAddUser()"
      >添加员工</el-button>
      <!-- <el-button
        size="mini"
      >删除</el-button> -->
    </el-row>
    <el-table
      :data="userList"
      class="app-flex-1"
      style="width: 100%;overflow-y: auto;"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
      /> -->
      <el-table-column
        prop="name"
        align="center"
        label="姓名"
        width="180"
      />
      <el-table-column
        prop="mobile"
        align="center"
        label="电话"
        width="180"
      />
      <el-table-column
        prop="role"
        align="center"
        label="角色权限"
        width="180"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.roleName || '/' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="userName"
        align="center"
        label="创建人"
      />
      <el-table-column
        prop="createTime"
        align="center"
        label="添加时间"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.createTime.replace('T',' ') }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mb-10 mt-5"
      background
      :current-page.sync="pageNo"
      :page-sizes="[10, 20, 50]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getUserList"
      @current-change="getUserList"
    />
    <el-dialog
      :title="isEdit? '修改员工' : '添加员工'"
      :visible.sync="addUserDialog"
      width="50vw"
    >
      <el-form ref="form" :rules="rules" class="app-width-80p" :model="form" label-width="160px">
        <el-form-item prop="name" label="员工姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item prop="mobile" label="员工电话">
          <el-input v-model="form.mobile" />
        </el-form-item>
        <el-form-item v-if="isEdit" prop="password" label="账号密码">
          <el-input v-model="form.password" show-password />
        </el-form-item>
        <el-form-item prop="roleId" label="员工角色">
          <el-select v-model="form.roleId" placeholder="请选择角色">
            <el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="auths.update_user" prop="createUser" label="创建人">
          <el-select v-model="form.userId" placeholder="创建人">
            <el-option v-for="u in creatorList" :key="u.userId" :label="u.username" :value="u.userId" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addUserDialog = false">取 消</el-button>
        <el-button type="info" @click="addUser()">确 定</el-button>
      </div>
    </el-dialog>
  </el-col>
</template>

<script>
import { getUserList,updateUser,deleteUserById,addUser,getRoleList } from '@/api/org-user'
import { getCustomerCreatorList } from '@/api/customer'
import { mapState } from 'vuex'
export default {
  data: () => ({
    rules: {
      name: [
        { required: true, message: '请输入员工姓名', trigger: 'blur' }
      ],
      mobile: [
        { required: true, message: '请输入员工电话', trigger: 'blur' }
      ],
      username: [
        { required: true, message: '请输入员工账号', trigger: 'blur' }
      ],
      roleId: [
        { required: true, message: '请选择员工角色', trigger: 'blur' }
      ]
    },
    isEdit: false,
    addUserDialog: false,
    userList: [],
    creatorList: [],
    roleList: [],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    form: {
      name: '',
      mobile: '',
      username: '',
      password: '123456',
      roleId: null,
      roleName: '',
      userId: null
    },
    auths: {
      update_user: false
    }
  }),
  computed: {
    ...mapState('app',[
      'menuAndAuth'
    ])
  },
  mounted() {
    const menu = this.menuAndAuth.find(m => m.name === '员工管理')
    if (menu) {
      this.auths.update_user = menu.children.some(auth => auth.path === 'update_user')
      this.getUserList()
      this.getRoleList()
      // 创建人列表
      this.getCreatorList()
    }
  },
  methods: {
    getCreatorList() {
      getCustomerCreatorList().then(res => {
        if (res.code === 200) {
          this.creatorList = res.data
        }
      })
    },
    getRoleList() {
      getRoleList().then(res => {
        if (res.code === 200) {
          this.roleList = res.data
        }
      })
    },
    toAddUser() {
      this.form = {
        name: '',
        mobile: '',
        username: '',
        password: '',
        roleId: null
      }
      this.isEdit = false
      this.addUserDialog = true
    },
    getUserList() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        name: this.name
      }
      getUserList(params).then(res => {
        if (res.code === 200) {
          this.userList = res.data.dataList
          this.total = res.data.total
        }
      })
    },
    handleEdit(index,row) {
      this.isEdit = true
      this.form = row
      delete this.form.password
      this.addUserDialog = true
    },
    handleDelete(index,row) {
      this.$confirm(`确认删除用户${row.name}？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserById({ id: row.id }).then(res => {
          if (res.code === 200) {
            this.$message({ type: 'success',message: '已删除' })
            this.pageNo = 1
            this.getUserList()
          }
        })
      }).catch(() => {
        this.$message('已取消')
      })
    },
    addUser() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.roleName = this.roleList.find(r => r.id === this.form.roleId).name
          // this.form.userName = this.userList.find(r => r.id === this.form.userId).name
          if (this.isEdit) {
            updateUser(this.form).then(res => {
              if (res.code === 200) {
                this.getUserList()
                this.getCreatorList()
                this.$message({ type: 'success',message: '操作成功' })
                this.addUserDialog = false
              }
            })
          } else {
            addUser(this.form).then(res => {
              if (res.code === 200) {
                this.pageNo = 1
                this.getUserList()
                this.this.getCreatorList()
                this.$message({ type: 'success',message: '操作成功' })
                this.addUserDialog = false
              }
            })
          }
        }
      })
    }
  }
}
</script>
